package com.ruoyi.web.controller.minglvmini;

import com.github.pagehelper.PageInfo;
import com.minglv.mini.domain.Need;
import com.minglv.mini.domain.NeedMsg;
import com.minglv.mini.domain.dto.MsgStatusDto;
import com.minglv.mini.domain.dto.NeedMsgVo;
import com.minglv.mini.domain.dto.NeedVo;
import com.minglv.mini.service.INeedMsgService;
import com.minglv.mini.service.INeedService;
import com.minglv.mini.service.IUserService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * 【请填写功能名称】Controller
 *
 * @author
 * @date 2025-08-10
 */
@Api(tags = "Amini-admin-需求列表")
@RestController
@RequestMapping("/system/need")
public class NeedController extends BaseController{

    @Autowired
    private INeedService needService;

    @Resource
    private INeedMsgService needMsgService;

    @Resource
    private IUserService userService;
    /**
     * 查询【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('system:need:list')")
    @GetMapping("/list")
    public TableDataInfo list(Need need)
    {
        startPage();
        List<Need> list = needService.selectNeedList(need);
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setTotal(new PageInfo(list).getTotal());
        if (!CollectionUtils.isEmpty(list)) {
            List<NeedVo> voList = new java.util.ArrayList<>();
            for (Need n : list) {
                NeedVo needVo = new NeedVo();
                BeanUtils.copyProperties(n, needVo);
                needVo.setUser(userService.selectUserById(n.getUserId()));
                List<NeedMsgVo> needMsgList = needService.getNeedMsgList(n.getId());
                needVo.setMsgNum(CollectionUtils.isEmpty(needMsgList) ? 0 : needMsgList.size());
                voList.add(needVo);
            }
            rspData.setRows(voList);
        }
        return rspData;
    }

    @GetMapping("/getNeedMsgList")
    @ApiOperation("根据需求id获得留言列表")
    public TableDataInfo getNeedMsgList(@RequestParam(value = "nid") @ApiParam(value = "需求id") Long nid) {
        startPage();
        List<NeedMsgVo> needMsgList = needService.getNeedMsgList(nid);
        return getDataTable(needMsgList);
    }

//    @GetMapping("/msgList")
//    public TableDataInfo msgList(@RequestParam(value = "id") Long id){
//        NeedMsg needMsg = new NeedMsg();
//        needMsg.setNid(id);
//        startPage();
//        List<NeedMsg> needMsgs = needMsgService.selectNeedMsgList(needMsg);
//        return getDataTable(needMsgs);
//    }

    @ApiOperation("是否发布者查看")
    @PostMapping("/msg/status")
    public AjaxResult msgStatus(@Validated @RequestBody @ApiParam(value = "是否发布者查看") MsgStatusDto dto) {
        NeedMsg needMsg = needMsgService.selectNeedMsgById(dto.getId());
        if (ObjectUtils.isNotEmpty(needMsg)) {
            needMsg.setIsshow(dto.getIsshow());
            return toAjax(needMsgService.updateNeedMsg(needMsg));
        }else{
            return AjaxResult.error();
        }
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('system:need:export')")
    @Log(title = "需求列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Need need)
    {
        List<Need> list = needService.selectNeedList(need);
        ExcelUtil<Need> util = new ExcelUtil<Need>(Need.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:need:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(needService.selectNeedById(id));
    }

    /**
     * 新增【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:need:add')")
    @Log(title = "需求列表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Need need)
    {
        return toAjax(needService.insertNeed(need));
    }

    /**
     * 修改【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:need:edit')")
    @Log(title = "需求列表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Need need)
    {
        return toAjax(needService.updateNeed(need));
    }

    /**
     * 删除【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:need:remove')")
    @Log(title = "需求列表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(needService.deleteNeedByIds(ids));
    }

}
